<template>
  <div>
    <p class="title">广播类型</p>
    <span class="zt">状态:</span>
    <el-select v-model="value" placeholder="请选择" size="small" class="ztxz">
      <el-option
        v-for="item in options"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      >
      </el-option>
    </el-select>
    <el-button
      type="primary"
      size="small"
      class="add"
      @click="centerDialogVisible = true"
      >新建</el-button
    >
    <!-- <el-button type="text" @click="centerDialogVisible = true">点击打开 Dialog</el-button> -->

    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column prop="btId" label="序号" width="80" align="center">
      </el-table-column>
      <el-table-column
        prop="btName"
        label="类型名称"
        width="180"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间" align="center">
      </el-table-column>
      <el-table-column prop="state" label="状态" align="center">
      </el-table-column>
      <el-table-column prop="address" label="操作" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="ck(scope.row)">编辑</el-link>
          <el-link type="primary" @click="ck(scope.row)">下架</el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Page
      :total="total"
      :pageNumber.sync="pageNumber"
      :pageSize.sync="pageSize"
      @current-change="changeCurrentPage"
      @size-change="handleSizeChange"
    >
    </Page>

    <!-- 模态框 -->
    <el-dialog
      title="新建"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
    >
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="类型名称：">
          <el-input v-model="form.name"></el-input>
        </el-form-item>

        <el-form-item label="创建时间：">
          <el-col :span="11">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="form.date1"
              style="width: 100%"
            ></el-date-picker>
          </el-col>
          <!-- <el-col class="line" :span="2">-</el-col>
    <el-col :span="11">
      <el-time-picker placeholder="选择时间" v-model="form.date2" style="width: 100%;"></el-time-picker>
    </el-col> -->
        </el-form-item>

        <el-form-item label="状态：">
          <el-radio-group v-model="form.resource">
            <el-radio label="上架"></el-radio>
            <el-radio label="下架"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button @click="close">取 消</el-button>
          <el-button type="primary" @click="add(form)">确 定</el-button>
        </el-form-item>
      </el-form>

      <!-- <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="add">确 定</el-button>
      </span> -->
    </el-dialog>
  </div>
</template>

<script>
import Page from "../../components/page.vue";
import apis from "../../apis/modules/broadcastType";
export default {
  components: {
    Page,
  },
  data() {
    return {
      //模态框表单
      form: {
        name: "",
        date1: "",
        resource: "",
      },

      centerDialogVisible: false, //模态框
      radio: "1",
      // 分页
      currentPage: 1,
      total: null,
      pageNumber: 1,
      pageSize: 10,
      //类型表格数据
      tableData: [],
      //下拉数据
      options: [
        {
          value: "all",
          label: "全部",
        },
        {
          value: "1",
          label: "已上架",
        },
        {
          value: "2",
          label: "已下架",
        },
      ],
      value: "all",
    };
  },
  mounted() {
    this.getBroadcastType({
      state: this.value,
      pageNumber: this.pageNumber,
      pageSize: this.pageSize,
    });
  },
  watch: {
    value() {
      switch (this.value) {
        case "all":
          this.getBroadcastType({
            state: this.value,
            pageNumber: this.pageNumber,
            pageSize: this.pageSize,
          });

          break;
        case "1":
          this.getBroadcastType({
            state: this.value,
            pageNumber: this.pageNumber,
            pageSize: this.pageSize,
          });

          break;
        case "2":
          this.getBroadcastType({
            state: this.value,
            pageNumber: this.pageNumber,
            pageSize: this.pageSize,
          });

          break;

        default:
          break;
      }
    },
  },
  methods: {
    //  获取广播类型列表
    async getBroadcastType(data) {
      await apis.getBroadcastType(data).then((res) => {
        console.log(res);
        this.total = res.data.count;
        this.tableData = res.data.list;
        const list = this.tableData;
        list.map((res) => {
          switch (res.state) {
            case "1":
              res.state = "上架";
              break;
            case "2":
              res.state = "下架";
              break;

            default:
              break;
          }
          return res;
        });
      });
    },
    
    //  新建
    close() {
      this.centerDialogVisible = false;
    },
    add(data) {
      this.centerDialogVisible = false;
      console.log(this.centerDialogVisible, data);
    },
    changeCurrentPage(val) {
      this.pageNumber = val;
      const list = {
        state: this.value,

        pageNumber: this.pageNumber,
        pageSize: this.pageSize,
      };
      this.getBroadcastType(list);
      console.log(val, 333);
    },
    //改变每页显示条数
    handleSizeChange(val) {
      this.pageSize = val;
      const list = {
        state: this.value,
        pageNumber: this.pageNumber,
        pageSize: this.pageSize,
      };

      this.getBroadcastType(list);
      console.log(val, 222);
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  margin-top: -10px;
  font-size: 14px;
  margin-bottom: 20px;
}
.zt {
  font-size: 14px;
  margin: 40px 10px 20px 20px;
}
.el-input--small .el-input__inner {
  height: 32px;
  line-height: 32px;
  width: 126px;
}
.add {
  width: 88px;
  float: right;
  margin-top: 40px;
}
.model {
  margin-top: 20px;
}
.radio {
  margin-left: 32px;
  display: inline-block;
}
.el-link {
  margin-left: 20px;
}
</style>